//
//  HomeEarningCell.m
//  LingQian
//
//  Created by xieyingze on 2018/1/21.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "HomeEarningCell.h"
#import "CustomAdView.h"

@interface HomeEarningCell ()
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) CustomAdView *adView;
@property (nonatomic, strong) BaseLabel *totalEarningNumberLabel;
@property (nonatomic, strong) BaseLabel *usableNumberLabel;
@property (nonatomic, strong) BaseLabel *totalAssetsNumberLabel;
@property (nonatomic, strong) BaseLabel *experienceNumberLabel;
@end

@implementation HomeEarningCell

+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    static NSString *cellId = @"HomeEarningCell";
    HomeEarningCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[HomeEarningCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    return cell;
}

#pragma mark - init

- (void)initData {
    self.backgroundColor = kCOLOR_clear;
}

- (void)initSubView {
    [self addSubview:self.mainView];
}

- (void)autoLayoutSubviews {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(10, 20, 10, 20));
    }];
}

- (void)bindViewModel {
    self.adView.bottomViewColor = kCOLOR_clear;
    self.adView.currentPageIndicatorTintColor = kCOLOR_white;
    self.adView.pageIndicatorTintColor = [kCOLOR_white colorWithAlphaComponent:0.5];
    self.adView.direction = KHScrollDirectionFromRight;
    self.adView.bottomViewHeight = 60;
    self.adView.timeInterval = 3.f;
    [self.adView startTimer];

    [self.adView setUpLocalImagesWithSource:@[@"home_today1",@"home_today1"] ClickHandler:^(NSInteger index, NSString *imgSrc, UIImage *img) {
        
    }];
}

#pragma mark - Method

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.mainView setPopScaleXYAnmationFrom:1.0 toValue:0.95];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.mainView setPopScaleXYAnmationFrom:0.95 toValue:1.0];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.mainView setPopScaleXYAnmationFrom:0.95 toValue:1.0];
}

#pragma mark - Interface


#pragma mark - Setter

- (void)setViewModel:(HomeViewModel *)viewModel {
    _viewModel = viewModel;
    [self refreshUI];
}

- (void)refreshUI {
    self.totalEarningNumberLabel.textColor = KCOLOR_mainColor;
    self.usableNumberLabel.textColor = KCOLOR_mainColor;
    self.totalAssetsNumberLabel.textColor = KCOLOR_mainColor;
    self.experienceNumberLabel.textColor = KCOLOR_mainColor;
    [self.totalEarningNumberLabel setViewShadow:[KCOLOR_mainColor colorWithAlphaComponent:0.6] offset:CGSizeMake(0, 0) radius:kUI_ShawRadius/5];
}

#pragma mark - Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        _mainView.layer.cornerRadius = kUI_LayerCorner;
        [_mainView setViewShadow:kCOLOR_shadowLight offset:CGSizeMake(0, 0) radius:kUI_ShawRadius];
        
        CGFloat w = (kUI_WIDTH - 40)/3;

        self.adView.layer.cornerRadius = kUI_LayerCorner;
        self.adView.layer.masksToBounds = YES;
        [_mainView addSubview:self.adView];
        [self.adView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(@0);
            make.height.equalTo(@160);
        }];
        
        UIView *whiteView = [UIView new];
        whiteView.backgroundColor = kCOLOR_white;
        [_mainView addSubview:whiteView];
        [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.adView.mas_bottom).offset(-10);
            make.left.right.equalTo(@0);
            make.height.equalTo(@10);
        }];
        
        //累计收益
        BaseLabel *totalEarningLabel = [BaseLabel new];
        [totalEarningLabel setLabelStyle:@"累计收益" textColor:KCOLOR_lightFont textFont:kFONT_mainFont(10) texrAlignment:NSTextAlignmentCenter];
        [_mainView addSubview:totalEarningLabel];
        [totalEarningLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(whiteView.mas_bottom).offset(10);
            make.centerX.equalTo(@0);
            make.height.equalTo(@10);
        }];
        
        BaseLabel *totalEarningNumberLabel = [BaseLabel new];
        [totalEarningNumberLabel setLabelStyle:@"3717.08" textColor:KCOLOR_heavyFont textFont:kFONT_mainBoldFont(20) texrAlignment:NSTextAlignmentCenter];
        self.totalEarningNumberLabel = totalEarningNumberLabel;
        [_mainView addSubview:totalEarningNumberLabel];
        [totalEarningNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(totalEarningLabel.mas_bottom).offset(10);
            make.centerX.equalTo(@0);
            make.height.equalTo(@20);
        }];
        
        //总资产
        BaseLabel *usableLabel = [BaseLabel new];
        [usableLabel setLabelStyle:@"可用余额" textColor:KCOLOR_lightFont textFont:kFONT_mainFont(8) texrAlignment:NSTextAlignmentCenter];
        [_mainView addSubview:usableLabel];
        [usableLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(totalEarningNumberLabel.mas_bottom).offset(20);
            make.left.equalTo(@0);
            make.width.equalTo(@(w));
            make.height.equalTo(@10);
        }];
        
        BaseLabel *usableNumberLabel = [BaseLabel new];
        [usableNumberLabel setLabelStyle:@"17.50" textColor:KCOLOR_heavyFont textFont:kFONT_mainFont(16) texrAlignment:NSTextAlignmentCenter];
        self.usableNumberLabel = usableNumberLabel;
        [_mainView addSubview:usableNumberLabel];
        [usableNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(usableLabel.mas_bottom).offset(5);
            make.centerX.equalTo(usableLabel);
            make.height.equalTo(@16);
        }];
        
        //可用余额
        BaseLabel *totalAssetsLabel = [BaseLabel new];
        [totalAssetsLabel setLabelStyle:@"总资产" textColor:KCOLOR_lightFont textFont:kFONT_mainFont(8) texrAlignment:NSTextAlignmentCenter];
        [_mainView addSubview:totalAssetsLabel];
        [totalAssetsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(usableLabel);
            make.left.equalTo(usableLabel.mas_right);
            make.width.equalTo(@(w));
            make.height.equalTo(usableLabel);
        }];
        
        BaseLabel *totalAssetsNumberLabel = [BaseLabel new];
        [totalAssetsNumberLabel setLabelStyle:@"3717.08" textColor:KCOLOR_heavyFont textFont:kFONT_mainFont(16) texrAlignment:NSTextAlignmentCenter];
        self.totalAssetsNumberLabel = totalAssetsNumberLabel;
        [_mainView addSubview:totalAssetsNumberLabel];
        [totalAssetsNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(usableNumberLabel);
            make.centerX.equalTo(totalAssetsLabel);
            make.height.equalTo(usableNumberLabel);
        }];
        
        //体验金
        BaseLabel *experienceLabel = [BaseLabel new];
        [experienceLabel setLabelStyle:@"体验金" textColor:KCOLOR_lightFont textFont:kFONT_mainFont(8) texrAlignment:NSTextAlignmentCenter];
        [_mainView addSubview:experienceLabel];
        [experienceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(totalAssetsLabel);
            make.left.equalTo(totalAssetsLabel.mas_right);
            make.width.equalTo(@(w));
            make.height.equalTo(totalAssetsLabel);
        }];
        
        BaseLabel *experienceNumberLabel = [BaseLabel new];
        [experienceNumberLabel setLabelStyle:@"500.00" textColor:KCOLOR_heavyFont textFont:kFONT_mainFont(16) texrAlignment:NSTextAlignmentCenter];
        self.experienceNumberLabel = experienceNumberLabel;
        [_mainView addSubview:experienceNumberLabel];
        [experienceNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(totalAssetsNumberLabel);
            make.centerX.equalTo(experienceLabel);
            make.height.equalTo(totalAssetsNumberLabel);
        }];
    }
    return _mainView;
}

- (CustomAdView *)adView {
    if (!_adView) {
        _adView = [[CustomAdView alloc] initWithFrame:CGRectMake(0, 0, kUI_WIDTH - 40, 160)];
    }
    return _adView;
}

@end
